﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fixx.Connector;

namespace FixxVS
{
	public partial class FilterSettings : UserControl
	{
		public delegate void FilterChangedHandler(object sender, FilterSettingsChangeArgs ca);
		public event FilterChangedHandler Change = delegate { };

		public bool Initializing { get; set; }

		public FilterSettings()
		{
			this.Initializing = true;
			InitializeComponent();
		}

		private void FilterSettings_Load(object sender, EventArgs e)
		{
			var store = new FixxPersistantStorage();
			if (store != null && store.Repository != null && store.Repository.FixxSettings != null)
			{
				var fs = store.Repository.FixxSettings;
				this.cbOnlyAssignedToMe.Checked = fs.OnlyIssuesAssignedToMe;
				this.chkIncludeResolved.Checked = fs.IncludeResolvedIssues;
				this.chkIncludeClosed.Checked = fs.IncludeClosedIssues;
				this.chkIncludeArchivedProjects.Checked = fs.IncludeArchivedProjects;
				this.Initializing = false;
			}
		}

		private void ThrowChangedEvent()
		{
			if (this.Initializing)
				return;

			this.Change(this, new FilterSettingsChangeArgs()
			{
				OnlyAssignedToMe = this.cbOnlyAssignedToMe.Checked,
				IncludeArchivedProjects = this.chkIncludeArchivedProjects.Checked,
				IncludeClosedIssues = this.chkIncludeClosed.Checked,
				IncludeResolvedIssues = this.chkIncludeResolved.Checked,
			});
		}

		private void GlobalFilter_CheckedChanged(object sender, EventArgs e)
		{
			this.ThrowChangedEvent();
		}

	}
}
